VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "AutoFill"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Integer

Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saArray() As Any) As Long

Private Declare Function AccessibleObjectFromWindow _
    Lib "oleacc" ( _
        ByVal hwnd As Long, _
        ByVal dwId As Long, _
        riid As GUID, _
        ppvObject As Object) _
As Long

Private Declare Function AccessibleChildren _
    Lib "oleacc" ( _
        ByVal parhwnd As IAccessible, _
        ByVal iChildStart As Long, _
        ByVal cChildren As Long, _
        rgvarChildren As Variant, _
        pcObtained As Long) _
As Long

Private Type GUID
    lData1            As Long
    nData2            As Integer
    nData3            As Integer
    abytData4(0 To 7) As Byte
End Type

Public Function SetFocus(hwnd As Long, parIndex As Integer, index As Integer)
    Dim tGUID As GUID
    Dim oIA As IAccessible
    
    With tGUID
        .lData1 = &H618736E0
        .nData2 = &H3C3D
        .nData3 = &H11CF
        .abytData4(0) = &H81
        .abytData4(1) = &HC
        .abytData4(2) = &H0
        .abytData4(3) = &HAA
        .abytData4(4) = &H0
        .abytData4(5) = &H38
        .abytData4(6) = &H9B
        .abytData4(7) = &H71
    End With
    AccessibleObjectFromWindow hwnd, 0, tGUID, oIA
    SetChildFocus oIA, parIndex, index
    Set oIA = Nothing
End Function

Public Function FindValue(hwnd As Long, parIndex As Integer, index As Integer) As String()
    Dim tGUID As GUID
    Dim oIA As IAccessible
    Dim ret() As String
    
    With tGUID
        .lData1 = &H618736E0
        .nData2 = &H3C3D
        .nData3 = &H11CF
        .abytData4(0) = &H81
        .abytData4(1) = &HC
        .abytData4(2) = &H0
        .abytData4(3) = &HAA
        .abytData4(4) = &H0
        .abytData4(5) = &H38
        .abytData4(6) = &H9B
        .abytData4(7) = &H71
    End With
    
    AccessibleObjectFromWindow hwnd, 0, tGUID, oIA
    ret = FindChild(oIA, parIndex, index)
    If SafeArrayGetDim(ret) = 0 Then
        ReDim ret(1) As String
        ret(0) = ""
        ret(1) = ""
    End If
    FindValue = ret
    Set oIA = Nothing
End Function

Private Function FindChild(ByRef oIA As IAccessible, parIndex As Integer, index As Integer) As String()
    Dim children() As Variant
    Dim childIA As IAccessible
    Dim ret() As String
    On Error Resume Next
    childrenNum = oIA.accChildCount
    If childrenNum = 0 Then
        Exit Function
    End If
    ReDim children(childrenNum - 1) As Variant
    AccessibleChildren oIA, 0, oIA.accChildCount, children(0), 0
    
    If parIndex < oIA.accChildCount And parIndex > -1 Then
        If IsObject(children(parIndex)) = True Then
            Set childIA = children(parIndex)
            ret = FindChild(childIA, -1, index)
            Set childIA = Nothing
        End If
    ElseIf index < oIA.accChildCount And index > -1 Then
        If IsObject(children(index)) = True Then
            ReDim ret(1) As String
            ret(0) = children(index).accName
            ret(1) = children(index).accValue
        End If
    End If
    FindChild = ret
    Set oIA = Nothing
End Function

Private Function SetChildFocus(ByRef oIA As IAccessible, parIndex As Integer, index As Integer)
    Dim children() As Variant
    Dim childIA As IAccessible
    On Error Resume Next
    childrenNum = oIA.accChildCount
    If childrenNum = 0 Then
        Exit Function
    End If
    ReDim children(childrenNum - 1) As Variant
    AccessibleChildren oIA, 0, oIA.accChildCount, children(0), 0
    
    If parIndex < oIA.accChildCount And parIndex > -1 Then
        If IsObject(children(parIndex)) = True Then
            Set childIA = children(parIndex)
            SetChildFocus childIA, -1, index
            Set childIA = Nothing
        End If
    ElseIf index < oIA.accChildCount And index > -1 Then
        If IsObject(children(index)) = True Then
            Set childIA = children(index)
            childIA.accSelect 1
        End If
    End If
    Set oIA = Nothing
    Set childIA = Nothing
End Function

